/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.RightEntity;
import java.util.List;
import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author thanhle
 */
@Stateless
public class RightEntityFacade extends AbstractFacade<RightEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public RightEntityFacade() {
        super(RightEntity.class);
    }

    public RightEntity findRightName(String name) {
        return getEntityManager().find(RightEntity.class, name);
    }

    public RightEntity findRight(String name) {
        try{
        Query query = (Query) em.createQuery(
                "SELECT r FROM rights r WHERE r.rightName = '" + name + "'");
        RightEntity result = (RightEntity) query.getSingleResult();
        
        return result;
        }catch(NoResultException e){
            return null;
        }
    }

    public List<RightEntity> findRights(String name) {
        Query query = (Query) em.createQuery(
                "SELECT r FROM rights r WHERE r.rightName = '" + name + "'");
        List<RightEntity> result = (List<RightEntity>) query.getResultList();

        return result;
    }
}
